package TCPEcho;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TCPEchoCline3 {
    Socket socket = null;
    public TCPEchoCline3(String severIp,int port) throws IOException {
        socket = new Socket(severIp,port);
    }
    public void start() throws IOException {
        System.out.println("客户端上线！！！");
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()){
            Scanner scannerNetwork = new Scanner(inputStream);
            Scanner scannerConsole = new Scanner(System.in);
            PrintWriter writer = new PrintWriter(outputStream);
            while(true){
                System.out.print("->");
                if(!scannerConsole.hasNext()){
                    break;
                }
                //从控制台读取请求
                String request = scannerConsole.next();
                //将请求发送给服务器
                writer.println(request);
                writer.flush();
                //接收从服务器返回的响应
                String response = scannerNetwork.next();
                //打印响应
                System.out.println(response);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        TCPEchoCline3 tcpEchoCline3 = new TCPEchoCline3("127.0.0.1",4545);
        tcpEchoCline3.start();
    }
}
